<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<%@ include file="/common/dxp/include/common-base.jsp"%>
	<title>单位管理</title>
	<!-- Tell the browser to be responsive to screen width -->
	<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">

</head>
<body class="hold-transition ${sysColor} sidebar-mini">
<div class="wrapper">
	<!-- top start -->
	<%@include file="/common/dxp/top.jsp" %>
	<!-- top end -->
	<!-- left start -->
	<%@include file="/common/dxp/left.jsp" %>
	<!-- left end-->



	<div class="content-wrapper">


		<!-- Main start -->
		<section class="content">
			<div class="row">
				<div class="col-xs-12">

					<div class="box box-info">
						<div class="box-header">
							<h3 class="box-title"><i class="fa fa-search"></i>单位管理搜索</h3>
							<div class="box-tools pull-right">
								<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
								</button>
							</div>
						</div>
						<!-- /.box-header -->
						<div class="box-body">

							<form id="searchItemForm" class="form-horizontal" action="javascript:;">
								<%--<input type="hidden" name="doCode" value="dictItemList"/><!-- ----- -->--%>
								<input type="hidden" name="pageName" value="dict-list-default"/>
								<input type="hidden" name="formName" value="searchItemForm"/>
								<input type="hidden" name="dictDataItem.w_fatherId" value="0"/>
								<input type="hidden" name="ifDataAct" value="F"/>
								<input type="hidden" name="dictDataItem.w_cateEname" id="form_cateEname" value="dxpUnit"/>
								<input type="hidden" name="dictDataItem.w_itemStatus" value="1"/>
								<input type="hidden" name="orderBy" value="dictDataItem.create_time desc"/>
								<input type="hidden" name="viewType"/>
								<table class="table table-bordered bordercolor">
									<tbody>
									<tr>
										<td class="active w150" align="right"><label
												class="form-control-static">单位名称：</label></td>
										<td>
											<div class="form-group">
												<input type="text" class="form-control clean"  name="dictDataItem.wl_itemText" id="itemText" value="" />
											</div>
										</td>
										<td class="active w150" align="right"><label
												class="form-control-static">单位标识：</label></td>
										<td>
											<div class="form-group">
												<input type="text" class="form-control clean" name="dictDataItem.wl_itemValue" id="itemValue" value="" />
											</div>
										</td>
									</tr>
									</tbody>
								</table>
								<center>
									<div class="box-footer clearfix">
										<button type="button" class="btn btn-success" onclick="getDataItems();"><i class="fa fa-search"></i> 查询</button>&nbsp;&nbsp;
										<button  type="button" class="btn btn-warning" onclick="mytools.cleanData('#searchItemForm');">重置 </button>
									</div>
								</center>
							</form>


						</div>
					</div>

					<div class="box box-success">
						<div class="box-header">
							<h3 class="box-title"><i class="fa fa-table"></i> 查询结果 [共计<span id="dataListTotalCount">0</span>条数据]
							<button class="btn btn-success" type="button" onclick="toItemAdd()">增加</button>
							</h3>
						</div>
						<!-- /.box-header -->
						<div class="box-body">
							<table id="tabItems" class="table table-bordered " >
								<thead>
								<tr>
									<th>序号</th>
									<th>单位名称</th>
									<th>单位标识</th>
									<th>描述</th>
									<th>创建时间</th>
									<th>操作</th>
								</tr>
								</thead>
								<tbody id="dictItmes_body"> </tbody>
							</table>
							<center>
								<ul id="dictItmes_pageList"></ul>
							</center>
						</div>
					</div>
				</div>
			</div>
		</section>

		<!-- Main end -->

		<div class="modal fade" id="my_modal_item" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
			<div class="modal-dialog" style="width:60%;">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
						<h4 class="modal-title"><font color="red">单位信息</font>&nbsp;<font color="red" id="span_dataItemForm_father_title"></font></h4>
					</div>
					<div class="modal-body">
						<form class="form-horizontal" role="form" id="dataItemForm" action="javascript:;">
							<input type="hidden" name="formToken" class="formToken" id="dataItemForm_formToken" value="${formToken}"/>
							<input type="hidden" name="pageName" value="dict-list-default"/>
							<input type="hidden" name="formName" value="addItemForm"/>
							<input type="hidden" name="dictDataItem.sysName" class="sysEname" value="${sysName}"/>
							<input type="hidden" name="dictDataItem.itemStatus" value="1"/>
							<input type="hidden" name="dictDataItem.subCount" class="clean" data-name="dictDataItem.subCount"/>
							<input type="hidden" name="dictDataItem.treeLevel" class="clean" data-name="dictDataItem.treeLevel"/>
							<input type="hidden" name="dictDataItem.cateEname" class="clean" data-name="dictDataItem.cateEname"  id="data-cateEname"/>
							<input type="hidden" name="dictDataItem.fatherId" class="clean" id="data-fatherId" data-name="dictDataItem.fatherId" value="0"/>
							<input type="hidden" name="dictDataItem.w_itemId" class="clean" data-name="dictDataItem.itemId" id="data-itemId" value=""/>
							<div class="box-body">
								<div class="form-group">
									<label  class="col-sm-2 control-label">单位名称</label>
									<div class="col-sm-8">
										<input type="text" class="form-control clean" data-name="dictDataItem.itemText" name="dictDataItem.itemText"  placeholder="必填项" data-bv-notempty="true" data-bv-notempty-message="不能为空" >
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">单位标识</label>
									<div class="col-sm-8">
										<input type="text" class="form-control clean" data-name="dictDataItem.itemValue" name="dictDataItem.itemValue"  placeholder="必填项,英文或数字标识"
											   data-bv-notempty="true" data-bv-notempty-message="不能为空"
											   data-bv-regexp="true" data-bv-regexp-regexp="^[a-zA-Z0-9-._]+$" data-bv-regexp-message="英文或数字标识">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-2 control-label">单位描述</label>
									<div class="col-sm-8">
										<input type="text" id="input_dictDataItem_itemRemark" class="form-control clean" data-name="dictDataItem.itemRemark"  name="dictDataItem.itemRemark" placeholder="">
									</div>
								</div>
							</div>
							<div class="box-footer">
								<button type="button" class="btn btn-danger pull-left" onclick="mytools.closeModal('my_modal_item');">取消</button>
								<button type="button" class="btn btn-success pull-right" onclick="saveItem('dataItemForm');">提 交</button>
							</div>
						</form>
					</div>
				</div>
				<!-- /.modal-content -->
			</div>
		</div>
		<script type="text/template" id="dataItemList">
			<? for ( var i = 0; i < rows.length; i++) {?>
			<tr id="<?=rows[i].dictDataItem.itemId?>" treeLevel="<?=rows[i].dictDataItem.treeLevel?>" fatherId="<?=rows[i].dictDataItem.fatherId?>">
				<th style="display:none;"><?=rows[i].dictDataItem.itemId?></th>
				<th style="display:none;"><?=rows[i].dictDataItem.treeLevel?></th>
				<th style="display:none;"><?=rows[i].dictDataItem.fatherId?></th>
				<td><?=(pageObj.currentPage-1)*10+(i+1)?></td>
				<td><?=rows[i].dictDataItem.itemText?></td>
				<td><?=rows[i].dictDataItem.itemValue?></td>
				<td><?=rows[i].dictDataItem.itemRemark?></td>
				<td><?=getFormatDateByLong(rows[i].dictDataItem.createTime,'yyyy-MM-dd hh:mm:ss')?></td>
				<td>
					<button type="button" class="btn btn-warning btn-xs" onclick="toItemEdit('<?=rows[i].dictDataItem.itemId?>','<?=rows[i].dictDataItem.itemValue?>');">修改</button>&nbsp;
					<button type="button" class="btn btn-danger btn-xs" onclick="delItem('<?=rows[i].dictDataItem.itemId?>','<?=rows[i].dictDataItem.cateEname?>','<?=rows[i].dictDataItem.itemValue?>','<?=rows[i].dictDataItem.fatherId?>');">删除</button>  
					<?if(viewType=='1'||viewType=='2'){?>
					<a href="javascript:void(0);" onclick="toSubItemAdd('<?=rows[i].dictDataItem.itemId?>','<?=rows[i].dictDataItem.itemValue?>','<?=rows[i].dictDataItem.fullEname?>')">增加子条目</a>
					<?}?>
				</td>
			</tr>
			<?}?>
		</script>

		<script>
			var clickMenu= 'unit-list';
			var id;
			var cateEname;
			var dictUrl = common_list_url+"?doCode=dictItemList";
			var defSysEname = strUtil.doNull('${sysName}','dxp');
			var itemId;
			var myPage=10;
			$(function () {
				// getTableData();
				getDataItems();
				$('#example tbody').on('click','tr', function (e) {
					if ( $(this).hasClass('selected') ) {
						//$(this).removeClass('selected');
					}  else {
						$('tr.selected').removeClass('selected');
						$(this).addClass('selected');
					}
					// id = $('tr.selected th').eq(0).html();
					// cateEname = $('tr.selected td').eq(1).html();
					//  console.log($('tr.selected td').eq(1).html())
				} );


			});

			//给模态窗口内form表单内的所有支持必填项的元素注入校验
			$(".modal").on('shown.bs.modal', function () {
				$(this).find("form").each(function () {
					var formId = $(this).attr("id");
					if (!strUtil.isNull(formId)) {
						formId = "#" + formId;
						setValidate(formId);//定义add表单验证属性
						mytools.cleanValidate(formId);//清空上次校验规则提示
					}
				});
			});

			//设置模态框隐藏事件钩子
			$( '#my_modal' ).on( 'hide.bs.modal' ,function(e){
				mytools.cleanData("#addForm");
			});

			//设置模态框隐藏事件钩子
			$( '#my_modal' ).on( 'show.bs.modal' ,function(e){
				$.ajax({ type:'post',url:'${ctx}/sendPage/token?token=true', dataType:'json',
					success: function(myData){
						//console.log(myData);
						$('.formToken').val(myData);
					}
				});
			});
			//弹框条目明细框
			//科目数据明细
			function getDataItems(){
				var index_load = layer.load(1, {shade: false});
				$.ajax({url:common_list_url,
					async:false,type: "get",
					data:$('#searchItemForm').serialize() + getCurrentPage(common_list_url + "?" + $("#searchItemForm").serialize(), myPage),
					dataType: "json",
					success: function(myData){
						//console.log(myData);
						layer.close(index_load);
						$("#dataListTotalCount").text(myData.total);
						myData.viewType = $("#searchItemForm").find("input[name='viewType']").val();
						var data = baidu.template('dataItemList',myData);
						// alert(data);
						$("#dictItmes_body").html(data);
						$('#dictItmes_body').on('click','tr', function (e) {
							if ( $(this).hasClass('selected') ) {
								//$(this).removeClass('selected');
							}else {
								$('tr.selected').removeClass('selected');
								$(this).addClass('selected');
							}
							itemId = $('tr.selected th').eq(0).html();
						} );
						var option= {ele:$("#dictItmes_pageList")[0],total:myData.total,fn:getDataItems,url:common_list_url+"?"+$('#searchForm').serialize()};
						generatePages(option);
					}
				});
			}

			function toEdit(cateEname,cateId){
				$.ajax(
						{   type:'get',
							url:common_info_url,
							dataType:'json',
							// data:{'cateId':id,'doCode':'dictCateInfo'},
							data:{'pageName':'dict-list','formName':'searchForm','dictDataCate.w_cateId':cateId},
							success: function(myData){
								// FormUtil.fillData("#addForm", myData,"id");
								FormUtil.loadView("dictDataCate", myData.dictDataCate);
								DictUtil.checkRadioDict("dictDataCate.cateStatus", myData.dictDataCate.cateStatus);
								$("#addForm").find("input[name='formName']").val("editForm");
								$("#addForm").find("select[name='dictDataCate.viewType']").val(myData.dictDataCate.viewType);
								$('#my_modal').modal('show');
								setToken();
							}
						});
				//$('#my_modal').modal('show');
			}

			function del(cateEname,cateId){
				var flag = checkUnitFlag(cateId);
				if(flag){
					layer.alert("此单位["+cateEname+"]下存在系统，请先删除系统后再执行此操作。");
					return;
				}
				layer.confirm('您确认要删除['+cateEname+']操作？', { btn: ['确认','取消'] },
						function(){
							$.ajax({type:'post', url:common_submit_url, dataType:'json',
								data:{'pageName':'dict-list','formName':'delForm','dictDataCate.w_cateId':cateId,'dictDataCate.w_cateEname':cateEname},
								success: function(myData){
									console.log("dictCateDel",myData);
									if(myData.flag=='T'){
										layer.alert('删除['+cateEname+']操作成功！');
										createDictJsFile("1","","");
										refresh();
									}else{
										layer.alert('操作失败！'+myData.msg);
									}
								}
							});
						},
						function(){}
				);
			}故无法
			function checkUnitFlag(cateId) {
				var flag=false;
				$.ajax({type:'get', url:common_list_url, dataType:'json',async:false,
					data:{'pageName':'dx-sys-reg','formName':'searchForm','dxSysReg.w_userOwner':cateId},
					success: function(myData){
					console.log(myData,"dx-sys-reg");
						if(myData.flag=='T'&&myData.total>0){
							flag=true;
						}
					}
				});
				return flag;
			}
			function save(formId){
				if(!$("#" + formId)[0].checkValidity()){
					layer.alert("请先完善必填项！");
					return ;
				}
				var index_submit = layer.confirm("您确定要保存信息吗？", {btn: ["Yes","No"],closeBtn: 0,offset: "220px"}
						, function(){
							layer.close(index_submit);
							$.ajax({ type:'post',
								url:common_submit_url,
								dataType:'json',
								data:$("#"+formId).serialize(),
								success: function(myData){
									//console.log(myData);
									if(myData.flag=='T'){
										$('#my_modal').modal('hide');
										createDictJsFile("1","","");
										refresh();
									}else{
										layer.alert(myData.msg);
									}
								}
							});
						});
			}

			function createDictJsFile(flag,cateEname,viewType) {
				var urlAddr = "${ctx}/common/coding/";
				var paramData={};
				paramData["sysEname"]=defSysEname;
				if(flag=="1"){
					urlAddr+="createDictCateList";
				}else{
					urlAddr+="createDictCateItemList";
					paramData["cateEname"]=cateEname;
					paramData["viewType"]=viewType;
				}
				$.ajax({ type:'post', url:urlAddr,dataType:'json',
					data:paramData,
					success: function(myData){
						console.log("createDictJsFile",myData);
						//layer.alert(myData.msg);
					}
				});
			}

			function cannel(){
				$('#my_modal').modal('hide');

			}
			function deleteAllRow(){
				var tb = document.getElementById('example');
				if(tb){
					var rowNum=tb.rows.length;
					for (i=1;i<rowNum;i++){
						tb.deleteRow(i);
						rowNum=rowNum-1;
						i=i-1;
					}
				}
			}

			function refresh(){
				//window.location.reload();
				// getTableData();
				getDataItems();
			}
			function setToken(){
				$.ajax({ type:'get',url:'${ctx}/sendPage/token?token=true',dataType:'text',
					success: function(myData){
						$('.formToken').val(myData);
					}
				});
			}

			function toItemEdit(itemId,itemValue){
				setToken();
				mytools.cleanData("#dataItemForm");
				var index_load = layer.load(1, {shade: false});
				$.ajax({type:'get',url:common_info_url,
					data:{"pageName":"dict-list-default","formName":"searchItemForm","dictDataItem.w_itemId":itemId,"ifDataAct":"F"},
					dataType:'json',
					success: function(myData){
						layer.close(index_load);
						if(myData){
							FormUtil.loadView("dictDataItem", myData.dictDataItem);
							$("#dataItemForm").find("input[name='formName']").val("editItemForm");
							$("#span_dataItemForm_father_title").text("[修改单位标识："+itemValue+"]");
							$('#my_modal_item').modal('show');
							$("#input_dictDataItem_itemRemark").attr("name","dictDataItem.s_itemRemark");
						}
					}
				});
			}
			function toItemAdd(){
				setToken();
				$("#span_dataItemForm_father_title").text("");
				mytools.cleanData("#dataItemForm");
				$("#dataItemForm").find("input[name='formName']").val("addItemForm");
				$("#dataItemForm").find("input[name='dictDataItem.treeLevel']").val("1");
				$("#span_dataItemForm_father_title").text("[新增]");
				$("#data-fatherId").val(0);
				$("#data-cateEname").val($("#form_cateEname").val());
				$("#input_dictDataItem_itemRemark").attr("name","dictDataItem.itemRemark");
				$('#my_modal_item').modal('show');
			}
			function toSubItemAdd(itemId,itemText,fullEname){
				setToken();
				var count = 1;
				if(fullEname){
					var ss = fullEname.split("/");
					count = ss.length;
				}
				mytools.cleanData("#dataItemForm");
				$("#dataItemForm").find("input[name='formName']").val("addItemForm");
				$("#dataItemForm").find("input[name='dictDataItem.treeLevel']").val(count);
				$("#span_dataItemForm_father_title").text("[上级条目："+itemText+"]");
				$("#data-fatherId").val(itemId);
				$("#data-cateEname").val($("#form_cateEname").val());
				$('#my_modal_item').modal('show');
			}
			function saveItem(formId){
				// if(!$("#" + formId)[0].checkValidity()){
				// 	alert("请先完善必输项和必选项！");
				// 	return ;
				// }

				var validate = $('#' + formId).data('bootstrapValidator').validate();
				if (!validate.isValid()) { //如果校验不通过就返回
					return;
				}

				var cateEname = $("#searchItemForm").find("input[name='dictDataItem.w_cateEname']").val();
				var viewType = $("#searchItemForm").find("input[name='viewType']").val();
				var index_submit = layer.confirm("您确定要保存信息吗？", {btn: ["Yes","No"],closeBtn: 0,offset: "220px"}
						, function(){
							layer.close(index_submit);
							$.ajax({ type:'post',url:common_submit_url,
								data:$("#"+formId).serialize(), dataType:'json',
								success: function(myData){
									if(myData.flag=='T'){
										layer.alert('操作成功！', {skin:'layui-layer-molv',closeBtn:0});
										$('#my_modal_item').modal('hide');
										createDictJsFile("2",cateEname,viewType);
										getDataItems();
									}else{alert(myData.msg);}
								}
							});
						})
			}

			function getChildTableData(id,treeLevel,obj){
				var count =0;
				var ss = treeLevel.split("/");
				count = ss.length-1;
				var cateNamestr=$("#form_cateEname").val();
				$.ajax({
					type:'get',
					url:common_list_url,
					data:{"pageName":"dict-list-default","formName":"searchItemForm","dictDataItem.w_cateEname":cateNamestr,"dictDataItem.w_fatherId":id},
					//data:{'id':id,"cateEname":cateNamestr,'doCode':'dictItemList'},
					dataType:'json',
					success: function(myData){
						myData.viewType = $("#searchItemForm").find("input[name='viewType']").val();
						var data = baidu.template('dataItemList',myData);
						if($(obj).hasClass("fa-folder-open")){
							$(obj).attr("class","fa fa-folder");
							delChiled(id);
						}else{
							$(obj).attr("class","fa fa-folder-open");
							$("#"+id).after(data);
							$("[fatherid='"+id+"']").each(function(i){
								var thisObj = $(this);
								var pd = "10px 20px 10px ";
								var num = pd+(count*20)+"px";
								$(thisObj).find("td").eq(0).css("padding",num);
							});
						}
					}
				});
			}
			function delChiled(id){
				$("[fatherid='"+id+"']").each(function(i){
					var thisObj = $(this);
					$(thisObj).remove();
					var id = thisObj.attr("id");
					delChiled(id);
				});

			}
			function delItem(itemId,cateEname,itemValue,fatherId){
				var flag = checkUnitFlag(itemValue);
				if(flag){
					layer.alert("此单位["+itemValue+"]下存在系统，请先删除系统后再执行此操作。");
					return;
				}
				layer.confirm('您确认要删除['+itemValue+']操作吗？', { btn: ['确认','取消'] },
						function(){
							$.ajax({type:'post',url:common_submit_url,
								data:{"pageName":"dict-list-default","formName":"delItemForm","dictDataItem.w_cateEname":cateEname,"dictDataItem.w_itemId":itemId,"dictDataItem.w_fatherId":fatherId},
								dataType:'json',
								success: function(myData){
									if(myData.flag=='T'){
										layer.alert('删除['+itemValue+']操作成功！', {skin: 'layui-layer-molv',closeBtn: 0 });
										createDictJsFile("2",cateEname,'');
										getDataItems();
									}else{
										layer.alert(myData.msg);
									}
								}
							});
						},
						function(){}
				);
			}
			/**** Part 四、以下是自定义新增方法  ****/
//1.定义校验规则-定义
			function setValidate(id) {
				$(id).each(function () {
					$(this).bootstrapValidator({
						feedbackIcons: {
							valid: 'glyphicon glyphicon-ok',
							invalid: 'glyphicon glyphicon-remove',
							validating: 'glyphicon glyphicon-refresh'
						}, /** 以下需要结合实际业务清空补充个表单元素的验证规则 **/
						fields: {
							/** 可自定义新增验证规则
							 "uctUser.userEname":{
		    		 validators: {
		    			 regexp: {
		    				 regexp: /^[0-9]+$/,
		                      message: '只能是数字'
		                  }
		              }
		    	}
							 **/
						}
					});
				});
			}

		</script>



	</div>

</div>
</body>








<!-- foot start -->

<%@include file="/common/dxp/foot.jsp" %>
<!-- foot start -->

</html>
